package com.coffee.core.util;

import java.util.Random;

/**
 * 生成随机字符串工具
 *
 * @author Xulg
 * Created in 2019-06-05 15:30
 */
public class CodeRandomGenerator {

    /**
     * 获取随机长度字符串
     *
     * @param length        字符串长度
     * @param includeLetter 是否包含字母
     * @return 长度为length的字符串
     */
    public static String getRandomString(int length, boolean includeLetter) {
        if (length < 1) {
            return "";
        }
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(getRandom());
        }
        return sb.toString();
    }

    /**
     * 生成6位随即短信码
     *
     * @return the code
     */
    public static int randomSMSCode() {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (999999 - 100000 + 1) + 100000;
    }

    /**
     * 随机获得指定的验证码
     *
     * @return char
     */
    private static char getRandom() {
        String value = "3479abdehkmnqxywABCEFGHJKLNPRTXY";
        int randomNumber = (int) (Math.random() * value.length());
        return value.toCharArray()[randomNumber];
    }

}
